<!-- 书架 -->
<script setup lang="ts">
import { getListpageNew, getMystories } from '@/api/getAxios'
import BookItem from '@/components/BookItem.vue';
import GetAd from '@/components/GetAd.vue';
import LastBook from '@/components/LastBook.vue';
import { closeToast, showLoadingToast } from 'vant';
import { toRaw } from 'vue';
import { ref } from 'vue'
import { useRouter } from 'vue-router';

const $router = useRouter()
const Newlist = ref()
const mystories = ref()
const storyId = ref()
showLoadingToast({
    duration: 0,
    forbidClick: true,
});
getListpageNew().then((res) => {
    Newlist.value = res.data.data.list
})
getMystories().then((res) => {
    mystories.value = res.data.data
    const result = Array.from(new Set(mystories.value.map(JSON.stringify)), <any>JSON.parse)
    mystories.value = result
    let data = toRaw(mystories.value).map((item: { storyVo: any; }) => {
        return item.storyVo.id
    })
    storyId.value = data
    if (localStorage.getItem("bookshelf") == null) {
        localStorage.setItem("bookshelf", JSON.stringify(data))
    } else {
        storyId.value = JSON.parse(<any>localStorage.getItem("bookshelf"))
    }
    closeToast()
})
$router.beforeEach((to) => {
    if (to.path != "/BookShelf") {
        document.oncontextmenu = function () {
        }
    } if (to.path == '/reading') {
        localStorage.setItem("news", <any>1)
    }
});
// 关闭右键菜单
document.oncontextmenu = function (e) {
    e.preventDefault();
}
</script>

<template>
    <div class="app">
        <header>
            <h1>📖 我的书架</h1>
            <div>
                <p @click="$router.push('/recently')"><img src="../../assets/img/历史.png" alt=""></p>
                <p @click="$router.push('/Search')"><img src="../../assets/img/搜索.png" alt=""></p>
            </div>
        </header>
        <div class="notice">
            <van-notice-bar left-icon="volume-o" :scrollable="false">
                <van-swipe vertical class="notice-swipe" :autoplay="4000" :show-indicators="false">
                    <van-swipe-item v-for="(item, index) in Newlist" :key="index"
                        @click="$router.push({ path: '/BookInfo', query: { storyId: item.story.id } })">{{
                            item.story.introduce }}
                    </van-swipe-item>
                </van-swipe>
            </van-notice-bar>
        </div>
        <LastBook />
        <div class="books">
            <BookItem v-for="item in storyId" :storyId="item" />
            <GetAd />
            <li class="add" @click="$router.push('/home');">
                <p>😍</p>
                <p>去看看</p>
            </li>
        </div>
    </div>
</template>


<style scoped lang="less">
.app {
    padding: 26px 10px 100px 10px;
    font-family: var(--mfont);

    header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;

        div {
            display: flex;
        }

        p {
            width: 32px;
            height: 32px;
            padding: 4px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 20px 0 gainsboro;
            transition: all 0.2s;
            margin: 0 4px;
            box-sizing: border-box;

            &:active {
                background-color: whitesmoke;
            }

            img {
                width: 100%;
            }
        }

        p:nth-child(1) {
            padding: 6px;
        }

        h1 {
            font-size: 1.2rem;
            display: flex;
            align-items: center;
            justify-content: space-between;

        }
    }


}

.notice-swipe {
    height: 40px;
    line-height: 40px;
}

.notice {
    margin-top: 10px;

    .van-notice-bar {
        background: white;
        color: whitesmoke;
        border-radius: 8px;

        .van-swipe-item {
            text-overflow: ellipsis;
            overflow: hidden;
            word-break: break-all;
            white-space: nowrap;
            color: black;
        }

        :deep(i) {
            color: black;
        }
    }
}


.van-nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}

.books {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    margin-top: 10px;
}

.add {
    background-color: white;
    width: 30%;
    border-radius: 10px;
    box-shadow: 0 0 10px 0 gainsboro;
    margin: 4px 1.6%;
    overflow: hidden;
    transition: all 0.2s;
    display: flex;
    min-height: 175px;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    transform: all 0.2s;

    p:nth-child(1) {
        font-size: 2.5rem;
    }

    p:nth-child(2) {
        color: #888;
        font-size: .85rem;
        margin-top: 24px;
    }

    &:active {
        background-color: whitesmoke;
    }
}

.van-nav-bar {
    background: transparent;
}
</style>